package org.dbstar.scheduler.actions;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.dbstar.scheduler.beans.CmdJobConf;
import org.dbstar.scheduler.beans.CompoundJobConf;
import org.dbstar.scheduler.beans.Job;
import org.dbstar.scheduler.beans.LoopJobConf;
import org.dbstar.scheduler.beans.OperateType;
import org.dbstar.scheduler.control.JobConfController;
import org.dbstar.scheduler.control.JobController;
import org.dbstar.scheduler.identifier.Identifier;
import org.dbstar.scheduler.job.JobStatus;


/**
 * 搜索系统中所有的Job
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-5-8
 */
public class JobSearchAction extends ActionSupport {
	// 仅接收
	private JobController jobController;

	// 仅传出
	private List<Job> jobs;
	private int jobSize;
	private int pageSize;
	private JobStatus statusList[];

	// 接收并传出
	private JobConfController jobConfController;
	private Identifier jobIdentifier;
	private Identifier confIdentifier;
	private String type;
	private JobStatus status;
	private String succeed;
	private String submitter;
	private String submitFrom;
	private String submitTo;
	private int page;

	// 仅接收
	public void setJobController(JobController jobController) {
		this.jobController = jobController;
	}

	// 仅传出
	public List<Job> getJobs() {
		return jobs;
	}

	public int getJobSize() {
		return jobSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public JobStatus[] getStatusList() {
		return statusList;
	}

	// 接收并传出
	public JobConfController getJobConfController() {
		return jobConfController;
	}

	public void setJobConfController(JobConfController jobConfController) {
		this.jobConfController = jobConfController;
	}

	public Identifier getJobIdentifier() {
		return jobIdentifier;
	}

	public void setJobIdentifier(Identifier jobIdentifier) {
		this.jobIdentifier = jobIdentifier;
	}

	public Identifier getConfIdentifier() {
		return confIdentifier;
	}

	public void setConfIdentifier(Identifier confIdentifier) {
		this.confIdentifier = confIdentifier;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public JobStatus getStatus() {
		return status;
	}

	public void setStatus(JobStatus status) {
		this.status = status;
	}

	public String isSucceed() {
		return succeed;
	}

	public void setSucceed(String succeed) {
		this.succeed = succeed;
	}

	public String getSubmitter() {
		return submitter;
	}

	public void setSubmitter(String submitter) {
		this.submitter = submitter;
	}

	public String getSubmitFrom() {
		return submitFrom;
	}

	public void setSubmitFrom(String submitFrom) {
		this.submitFrom = submitFrom;
	}

	public String getSubmitTo() {
		return submitTo;
	}

	public void setSubmitTo(String submitTo) {
		this.submitTo = submitTo;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	@Override
	public String execute() throws Exception {
		this.statusList = JobStatus.values();
		if (submitFrom == null) {
			// 默认设置开始时间为当天
			submitFrom = DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd");
		}
		Long submitFromLong = parseStartTime(submitFrom);
		Long submitToLong = parseEndTime(submitTo);
		String jobType = CmdJobConf.JOB_TYPE.equals(type) || CompoundJobConf.JOB_TYPE.equals(type)
				|| LoopJobConf.JOB_TYPE.equals(type) ? type : null;
		Boolean suc = StringUtils.isBlank(succeed) ? null : Boolean.valueOf(succeed);
		String jobSubmitter = StringUtils.isBlank(submitter) ? null : submitter;
		this.jobSize = jobController.searchJobSize(jobIdentifier, confIdentifier, jobType, status, suc, jobSubmitter,
				submitFromLong, submitToLong);
		this.pageSize = (jobSize + 9) / 10;
		this.jobs = jobController.searchJobs(jobIdentifier, confIdentifier, jobType, status, suc, jobSubmitter,
				submitFromLong, submitToLong, page, 10);
		operate(OperateType.job_search, String.valueOf(jobSize));
		return SUCCESS;
	}

	private static Long parseStartTime(String str) {
		if (!StringUtils.isBlank(str)) {
			try {
				return new SimpleDateFormat("yyyy-MM-dd").parse(str).getTime();
			} catch (Throwable e) {
			}
		}
		return null;
	}

	private static Long parseEndTime(String str) {
		if (!StringUtils.isBlank(str)) {
			try {
				return new SimpleDateFormat("yyyy-MM-dd").parse(str).getTime() + 24 * 3600000L;
			} catch (Throwable e) {
			}
		}
		return null;
	}
}
